package com.bawei.module_advice.http


import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.module_advice.model.AdviceModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class AdviceViewModel @Inject constructor(private val repository: AdviceRepository): BaseViewModel() {


    val adviceList= mutableListOf<AdviceModel>()


    private val _mAdvice= MutableStateFlow<RequestEvent>(Loading)
    val noticeState =_mAdvice.asStateFlow()

    fun getAdvice(page:Int,size:Int){
        //1
        execute(
            action = repository.getAdvice(page,size),
            onStart = {
                _mAdvice.value= Loading
            },
            onError = {
                _mAdvice.value= Failed(it)
            },
            onFinish = {
                adviceList.clear()
                adviceList.addAll(it.data)
                _mAdvice.value= Success
            }
        )

    }

}